<template>
  <div class="center">
    <!-- 头部 -->
    <div class="table_header flex flex_bet flex-wrap">
      <div class="table_button">
        <el-button type="primary" icon="el-icon-refresh" size="mini" @click="Refresh"></el-button>
      </div>
      <div class="table_searchs flex">
        <div class="table_search flex">
          <el-input placeholder="搜索" v-model="searchTable" class="input-with-select" size="small">
            <el-select v-model="select" slot="prepend" placeholder="请选择" class="table_select" style="width:105px">
              <el-option label="用户名" value="1"></el-option>
            </el-select>
            <el-button slot="append" icon="el-icon-search" @click="idnameSearch"></el-button>
          </el-input>
        </div>
        <dynamicTable :checkboxVal="checkboxVal" @alter="alterCheckbok" />
      </div>
    </div>
    <!-- 专题管理table -->
    <el-table stripe :data="brandTable" style="width: 100%" :key="key" border fit highlight-current-row ref="tableDataRef">
      <el-table-column v-if="colData[0].istrue" prop="username" label="创建者" min-width="120" align="center"></el-table-column>
      <el-table-column v-if="colData[1].istrue" prop="model_name" label="中文型号名" min-width="120" align="center"></el-table-column>
      <el-table-column v-if="colData[2].istrue" prop="digits_str" label="SN前五位" min-width="120" align="center"></el-table-column>
      <el-table-column v-if="colData[3].istrue" prop="ean_code" label="EAN商品码" min-width="120" align="center"></el-table-column>
      <el-table-column v-if="colData[4].istrue" prop="sn_num" label="数量" min-width="120" align="center"></el-table-column>
      <el-table-column v-if="colData[5].istrue" prop="address" label="历史生成地址" min-width="120" align="center">
          <template slot-scope="scope">
          <div class="flex flex_cen">
            <input type="text" class="site_inputs download_icon" :value="scope.row.address" />
            <i class="el-icon-share download download_icons">
              <a target="_blank" :href="scope.row.address"></a>
            </i>
          </div>
        </template>
      </el-table-column>
      <el-table-column v-if="colData[5].istrue" prop="create_time" label="创建时间" min-width="150" align="center">
        <template slot-scope="scope">{{scope.row.create_time| newDate}}</template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <div class="paging">
      <el-pagination :current-page.sync="listQuery.startSize" :page-sizes="[10, 50, 100, 200]" :page-size="listQuery.pageSize" layout="total, sizes, prev, pager, next, jumper" :total="total" background @size-change="handleSizeChange" @current-change="handleCurrentChange"></el-pagination>
    </div>
  </div>
</template>

<script>
import dynamicTable from "@/components/DynamicTable";
import objectable from "@/utils/objectable.js";
import deleteDialog from "@/components/deleteDialog";
import permission from '@/directive/permission/index.js' // 权限判断指令
import { getCreateSnLog } from "@/api/snControl.js";
export default {
  directives: { permission },
  components: { dynamicTable, deleteDialog },
  data() {
    return {
      checkboxVal: ['创建者', '中文型号名', 'SN前五位','EAN商品码', '数量', '历史生成地址', '创建时间'],
      key: 1,
      colData: [],//动态控制table
      brandTable: [],
      select: "", //搜索条件
      searchTable: "",
      total: 0, //总数量
      //添加按钮控制
      dialogFormVisible: false,
      //编辑按钮控制
      editSubjects: true,
      //编辑专题id
      subjectId: 0,
      listQuery: { startSize: 1, pageSize: 10, username: undefined, userId: localStorage.getItem("ID") }
    }
  },
  created() {
    this.colData = objectable(this.checkboxVal, this.checkboxVal);//转为数组对象，好控制table
    this.getCreateSnLogs()
  },
  methods: {
    getCreateSnLogs() {
      getCreateSnLog(this.listQuery).then(res => {
        this.brandTable = res.data
        this.total = res.total
      }).catch(err => {
        this.brandTable = []
        this.total = 0
      })
    },
    //分页
    handleSizeChange(val) {
      this.listQuery.pageSize = val
      this.getCreateSnLogs();
    },
    handleCurrentChange(val) {
      this.listQuery.startSize = val
      this.getCreateSnLogs();
    },
    //搜索,1，专题名称
    idnameSearch() {
      if (!this.select) return this.$message.warning("请先选择查询条件");
      this.listQuery.startSize = 1
      this.listQuery.pageSize = 10
      switch (this.select) {
        case '1':
          this.listQuery.username = this.searchTable
          this.getCreateSnLogs();
          break;
      }
    },
    Refresh() {
      this.listQuery = { startSize: 1, pageSize: 10, username: undefined, userId: localStorage.getItem("ID") }
      this.getCreateSnLogs()
    },
    //表单关闭事件
    addFormclose() {
      this.$nextTick(() => {
        this.$refs.ruleForm.resetFields();
      })
    },
    //下拉动态table子组件
    alterCheckbok(key, conte) {
      this.key = key;
      this.colData.filter(i => {
        if (conte.indexOf(i.title) != -1) {
          i.istrue = false;
          this.$nextTick(() => {
            this.$refs.tableDataRef.doLayout();
          });
        } else {
          i.istrue = true;
          this.$nextTick(() => {
            this.$refs.tableDataRef.doLayout();
          });
        }
      });
    }
  }
}
</script>